package projetofinal;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnectionFactory {

    private Connection conexao;

    public Connection getConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            conexao = DriverManager.getConnection("jdbc:derby://localhost:1527/projetoTeste", "root", "123");
            return conexao;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public void close() throws SQLException {
        if (this.conexao != null) {
            this.conexao.close();
        }
    }

    public ResultSet executar(String query) throws SQLException {
        Statement st;
        ResultSet rs;
        this.getConnection();
        st = this.conexao.createStatement();
        rs = st.executeQuery(query);
        return rs;
    }

    public int executarNoQuery(String query) throws SQLException {
        Statement st;
        int rs = 0;
        this.getConnection();
        st = this.conexao.createStatement();
        rs = st.executeUpdate(query);
        return rs;
    }
}
